/**
 * @author wangxiaowei 组织部发文列表
 */
var sort = null;
var grid = null;
var typestore = null;
var biaoti = '查阅文件';
function initzuzhibufawenlist() {

	Ext.BLANK_IMAGE_URL = Ext.get('basePath').getValue()
			+ "ajaxFramework/ext-2.1/resources/images/default/s.gif";

	Ext.menu.RangeMenu.prototype.icons = {
		gt : './ajaxFramework/ext-2.1/img/greater_then.png',
		lt : './ajaxFramework/ext-2.1/img/less_then.png',
		eq : './ajaxFramework/ext-2.1/img/equals.png'
	};
	Ext.grid.filter.StringFilter.prototype.icon = './ajaxFramework/ext-2.1/img/find.png';

	var colModel = new Ext.grid.ColumnModel([ {
		header : '年度',
		dataIndex : 'wenyear',
		width : 25,
		sortable : true
	}, {
		header : '发文号',
		dataIndex : 'wenhaotype'
	}, {
		header : '标题',
		dataIndex : 'biaoti'
	}, {
		header : '日期',
		dataIndex : 'fawendatetime',
		menuDisabled : true,
		width : 55,
		sortable : true
//	}, {
//		header : '文件类型',
//		dataIndex : 'wentype'
	}, {
		header : '操作',
		dataIndex : 'operate',
		menuDisabled : true
	} ]);

	var datearray = new Array();
	var min = parseInt($("input[name=min]").attr("value"));
	var max = parseInt($("input[name=max]").attr("value"));
	//alert(min);
	//alert(max);
	for ( var i = min; i <= max; i++) {
		datearray.push(i.toString());
	}

//	/* 动态获取文件种类 */
//	var typeurl = Ext.get('typeurl').getValue()+'?itemtype=xingzheng';
//	typestore = new Ext.data.JsonStore({
//		fields : [ 'id', 'typeName' ],
//		url : typeurl
//	});
//	
//	typestore.load();

	var filters = new Ext.grid.GridFilters({
		cleanParams : function(p) { // 清除参数的方法，要保留PagingToolbar分页用的两个参数
			// var regex = new RegExp("^" + this.paramPrefix + "\[[0-9]+\]");
			for ( var key in p) {
				if (key != 'start' && key != 'limit')
					// if(regex.test(key)) {
					delete p[key];
				// }
			}
		},
		buildQuery : function(filters) {
			// filters 就是getFilterData返回的值, 代码中push入去的对象
			var p = {};
			for ( var i = 0, len = filters.length; i < len; i++) {
				var f = filters[i];

				// 取得在GridFilters中定义的filters中的一个filter
				var filter = this.getFilter(f.field);
				// 取得传递到server的参数名称
				var name = (filter.paramName ? filter.paramName
						: filter.dataIndex);
				var value = (filter.paramValue ? filter.paramValue
						: f.data['value']);
//				if (name=="wentype"){
//					var i =value-1001;
//					value=typestore.getAt(i).get('typeName');
//				}
				if (value != '')
					p[name] = value;
			}
			var maintypeid = Ext.get('maintypeid').getValue();
			p['maintypeid']=maintypeid;
			var subtypename = Ext.get('subtypename').getValue();
			p['subtypename']=subtypename;
			return p;
		},
		filters : [ {
			type : 'string',
			dataIndex : 'biaoti'
		}, {
			type : 'string',
			dataIndex : 'wenhaotype'
		}, {
			type : 'list',
			dataIndex : 'wenyear',
			options : datearray
//		}, {
//			type : 'list',
//			dataIndex : 'wentype',
//			options : [],
//			store : typestore,
//			labelField : 'typeName',
//			single : true
		}, {
			type : 'list',
			dataIndex : 'zhuangtai',
			options : [ '已阅', '未阅', '' ]
		} ]
	});

	var url = Ext.get('url').getValue();
	sort = new Ext.data.JsonStore({
		url : url,
		fields : [ 'wenyear', 'wenhaotype', 'biaoti', 'fawendatetime',
				'wentype', 'zhuangtai', 'operate' ],
		root : 'root',
		totalProperty : 'totalProperty'
	});

	var bbar = new Ext.PagingToolbar({
		pageSize : 20,
		store : sort,
		displayInfo : true,
		plugins : filters,
		emptyMsg : '没有记录'
	});
	    
	 
	var maintypeurl = Ext.get('maintypeurl').getValue();
	var maintypestore = new Ext.data.JsonStore({
		fields : [ 'id', 'typeName' ],
		url : maintypeurl,
		listeners: {
        	load: function() {
        		 var combo=Ext.getCmp('maintypecombo'); 
                combo.setValue(this.getAt(0).get("typeName"));
      	}} 
	});
	maintypestore.load();
	
	var subtypeurl = Ext.get('subtypeurl').getValue();
	var subtypestore = new Ext.data.JsonStore({
		fields : [ 'id', 'typeName' ],
		url : subtypeurl,
		listeners: {
        	load: function() {
        		 var combo=Ext.getCmp('subtypecombo'); 
                combo.setValue(this.getAt(0).get("typeName"));
                document.getElementById("subtypename").value=this.getAt(0).get("typeName");
				//var maintypeid = Ext.get('maintypeid').getValue();
	    			//var subtypename = this.getAt(0).get("typeName");
	    	//		alert(maintypeid+"--"+subtypename);
	    			sort.reload({
	    				params : {
	    					//maintypeid:maintypeid,
	    					//subtypename:subtypename,
	    					start : 0,
	    					limit : 20
	    				}
	    			});
      	}} 
	});
	subtypestore.load({params:{mainTypeId:1}} );


	    var maintypecombo = new Ext.form.ComboBox({
	    	id:'maintypecombo',
	    	name:'maintype',
	        store: maintypestore,
	        displayField:'typeName',
	        typeAhead: true,
	        mode: 'local',
	        forceSelection: true,
	        triggerAction: 'all',
	        emptyText:'tt',
	        selectOnFocus:true,
	        width:120 ,
	        renderTo : 'maincombo',
	        listeners: {
			    select: function(combo, record, index){ 
			    	subtypestore.load({params:{mainTypeId:record.get("id")}} );
			    	document.getElementById("maintypeid").value=record.get("id");
			    }
	        }
	    });
	    var subtypecombo = new Ext.form.ComboBox({
	    	id:'subtypecombo',
	    	name:'subtype',
	        store: subtypestore,
	        displayField:'typeName',
	        typeAhead: true,
	        mode: 'local',
	        forceSelection: true,
	        triggerAction: 'all',
	        emptyText:'tt',
	        selectOnFocus:true, 
	        width:260 ,
	        renderTo : 'subcombo',
	        listeners: {
			    select: function(combo, record, index){ 
			    	document.getElementById("subtypename").value=record.get("typeName");
					var maintypeid = Ext.get('maintypeid').getValue();
	    			sort.reload({
	    				params : {
	    					maintypeid:maintypeid,
	    					subtypename:record.get("typeName"),
	    					start : 0,
	    					limit : 20
	    				}
	    			});
			    }
	        }
	    });
//	    var searchbutton = new Ext.Button({ 
//	    	renderTo:'searchbutton', 
//	    	text:"确定", 
//	    	listeners:{ 
//	    		"click":function() { 
//	    			var maintypeid = Ext.get('maintypeid').getValue();
//	    			var subtypename = Ext.get('subtypename').getValue();
//	    	//		alert(maintypeid+"--"+subtypename);
//	    			sort.reload({
//	    				params : {
//	    					maintypeid:maintypeid,
//	    					subtypename:subtypename,
//	    					start : 0,
//	    					limit : 20
//	    				}
//	    			});
//	    		} 
//	    	} 
//	    });
		
	
	grid = new Ext.grid.GridPanel({
		
		cm : colModel,
		renderTo : 'topic-grid',
		store : sort,
		loadMask : true,
		height : $(window).height() - 55,
		bbar : bbar,
		plugins : filters,
		viewConfig : {
			forceFit : true,
			getRowClass : function(record, rowIndex, p, ds) {
				if (record.get('state') == '是') {

					return 'green-row';
				} else if (record.get('state') == '中止') {

					return 'gray-row';
				}
			}
		}
	});
//	sort.load({
//		params : {
//			start : 0,
//			limit : 20
//		//	maintypeid : '1',
//		//	subtypename : '天津市粮油集团有限公司文件'
//		}
//	});
}
